//
//  Control_FunViewController.m
//  Control Fun
//
//  Created by 厳 中男 on 10/08/14.
//  Copyright team-lab 2010. All rights reserved.
//

#import "Control_FunViewController.h"

@implementation Control_FunViewController
@synthesize nameField;
@synthesize numberField;
@synthesize	sliderLable;
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize doSomethingButton;

- (IBAction)textFieldDoneEditing:(id)sender{
	[sender resignFirstResponder];
}

- (IBAction)backgroundTap:(id)sender{
	[nameField resignFirstResponder];
	[numberField resignFirstResponder];
}

- (IBAction)sliderChanged:(id)sender{
	UISlider *slider = (UISlider *)sender;
	int progressAsInt =(int)(slider.value + 0.5f);
	NSString *newText =[[NSString alloc] initWithFormat:@"%d",progressAsInt];
	sliderLable.text= newText;
	[newText release];
}

- (IBAction)toggleControls:(id)sender{
	if ([sender selectedSegmentIndex]==kSwitchesSegmentIndex) {
		leftSwitch.hidden=NO;
		rightSwitch.hidden=NO;
		doSomethingButton.hidden=YES;
	}
	else {
		leftSwitch.hidden=YES;
		rightSwitch.hidden=YES;
		doSomethingButton.hidden=NO;
	}

}
- (IBAction)switchChanged:(id)sender{
	UISwitch *whichSwitch=(UISwitch *)sender;
	BOOL setting = whichSwitch.isOn;
	[leftSwitch setOn:setting animated:YES];
	[rightSwitch setOn:setting animated:YES];
}
- (IBAction)buttoPressed{
	// TODO: Implement ction Sheet and Alert
	UIActionSheet *actionSheet = [[UIActionSheet alloc]
								  initWithTitle:@"Are you sure?"
								  delegate:self
								  cancelButtonTitle:@"No Way!" 
								  destructiveButtonTitle:@"Yes, I'm Sure!"
								  otherButtonTitles:nil];
	[actionSheet showInView:self.view];
	[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionsheet
didDismissWithButtonIndex:(NSInteger)buttonIndex{
	if (buttonIndex != [actionsheet cancelButtonIndex]) {
		NSString *msg = nil;
		
		if (nameField.text.length >0) {
			msg=[[NSString alloc] initWithFormat:
				 @"You can breathe easy, %@, everthing went OK.",
				 nameField.text];
		}
		else {
			msg=@"You can breathe easy, evething went OK.";
		}
		
		UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Something was done" 
							message:msg
							delegate:self
							cancelButtonTitle:@"Phew!"
							otherButtonTitles:nil];
		[alert show];
		[alert release];
		[msg release];

	}
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.nameField=nil;
	self.numberField=nil;
	self.sliderLable=nil;
	self.leftSwitch=nil;
	self.rightSwitch=nil;
	self.doSomethingButton=nil;
	[super viewDidUnload];
}


- (void)dealloc {
	[nameField release];
	[numberField release];
	[sliderLable release];
	[leftSwitch release];
	[rightSwitch release];
	[doSomethingButton release];
    [super dealloc];
}

@end
